package com.tianji.learning.controller;


import com.tianji.common.utils.BeanUtils;
import com.tianji.common.utils.CollUtils;
import com.tianji.learning.domain.po.PointsBoardSeason;
import com.tianji.learning.domain.query.PointsBoardQuery;
import com.tianji.learning.domain.vo.PointsBoardSeasonVO;
import com.tianji.learning.domain.vo.PointsBoardVO;
import com.tianji.learning.service.IPointsBoardSeasonService;
import com.tianji.learning.service.IPointsBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 学霸天梯榜 前端控制器
 * </p>
 *
 * @author xiaohai
 * @since 2024-09-18
 */
@RestController
@RequestMapping("/boards")
@RequiredArgsConstructor
@Api(tags = "积分排行榜相关接口")
public class PointsBoardController {

    private final IPointsBoardSeasonService seasonService;
    private final IPointsBoardService boardService;

    @GetMapping
    @ApiOperation("根据赛季查询积分排行榜")
    public PointsBoardVO queryPointsBoardBySeason(PointsBoardQuery query) {
        return boardService.queryPointsBoardBySeason(query);
    }

    @GetMapping("/seasons/list")
    @ApiOperation("查询赛季列表")
    public List<PointsBoardSeasonVO> queryPointsBoardSeasons() {
        // 1.获取时间
        LocalDateTime now = LocalDateTime.now();
        // 2.查询赛季列表，必须是当前赛季之前的(开始时间小于等于结束时间)
        List<PointsBoardSeason> list = seasonService.lambdaQuery()
                .le(PointsBoardSeason::getBeginTime, now)
                .list();
        if (CollUtils.isEmpty(list)) {
            return CollUtils.emptyList();
        }
        // 返回vo
        return BeanUtils.copyList(list, PointsBoardSeasonVO.class);
    }
}
